<template>
  <div>
    <el-dialog
      width="1024px"
      :close-on-click-modal="false"
      :show-close="true"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ dataForm.id != null ? '修改回款' : '新增回款' }}
      </div>
      <div class="sketch_content">
        <el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="100px"
                 label-position="top"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="回款编号" prop="receivablesSn">
                <el-input v-model="dataForm.receivablesSn" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerId">
                <el-input v-model="dataForm.customerName" placeholder="请选择客户名称" :style="{width: '100%'}"
                          @click.native="customerIdClick"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="合同编号" prop="contractSn">
                <el-input v-model="dataForm.contractSn" placeholder="请选择合同" :disabled="dataForm.customerId == null"
                          prefix-icon="el-icon-mobile" :style="{width: '100%'}" @click.native="contractIdClick"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回款日期" prop="returnTime">
                <el-date-picker v-model="dataForm.returnTime" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss" type="datetime"
                                :style="{width: '100%'}" placeholder="选择回款日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="回款金额" prop="money">
                <el-input v-model="dataForm.money" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="期数" prop="num">
                <el-select v-model="dataForm.num" placeholder="请选择" :style="{width: '100%'}"></el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="回款方式" prop="returnTypeId">
                <el-select v-model="dataForm.returnTypeId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_return_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="dataForm.remark" type="textarea" show-word-limit
                          :autosize="{minRows: 3, maxRows: 5}" :style="{width: '100%'}"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户选择 -->
    <customer-select-popover ref="customerSelectPopover" @callback="customerDataCallback"/>
    <!-- 合同选择 -->
    <contract-select-popover ref="contractSelectPopover" @callback="contractDataCallback"/>
  </div>
</template>
<script>
import {
  saveHasKey,
  selectByPrimaryKey,
  updateByPrimaryKeySelective
} from '@/api/crm/Receivables'

import CustomerSelectPopover from '@/components/Crm/Customer/CustomerSelectPopover'
import ContractSelectPopover from '@/components/Crm/Contract/ContractSelectPopover'

export default {
  inheritAttrs: false,
  components: {
    CustomerSelectPopover,
    ContractSelectPopover
  },
  dicts: ['crm_return_type'],
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      submitLoading: false,
      dataForm: {
        id: null, // 主键
        customerName: '', // 客户名称
        customerId: null, // 客户id
        contractSn: '', // 合同编号
        contractId: null, // 合同id
        contractName: '', // 合同编号
        receivablesSn: '', // 回款编号
        returnTime: null, // 回款日期
        money: null, // 回款金额
        num: null, // 期数
        returnTypeId: null, // 回款方式
        remark: null // 备注
      },
      rules: {
        customerId: [{ required: true, message: '请选择客户', trigger: 'change' }],
        contractSn: [{ required: true, message: '请选择合同', trigger: 'change' }],
        returnTime: [{ required: true, message: '请选择回款日期', trigger: 'blur' }],
        money: [{ required: true, message: '请输入回款金额', trigger: 'blur' }],
        returnTypeId: [{ required: true, message: '请选择回款方式', trigger: 'change' }]
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    // 对话框打开
    onOpen() {
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.dataForm = res.data
        })
      }
    },
    // 对话框关闭
    onClose() {
      this.submitLoading = false
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    reset() {
      this.dataForm = {
        id: null, // 主键
        customerName: '', // 客户名称
        customerId: null, // 客户id
        contractSn: '', // 合同编号
        contractId: null, // 合同id
        contractName: '', // 合同编号
        receivablesSn: '', // 回款编号
        returnTime: null, // 回款日期
        money: null, // 回款金额
        num: null, // 期数
        returnTypeId: null, // 回款方式
        remark: null // 备注
      }
    },
    submitForm() {
      this.submitLoading = true
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.dataForm.id != null) {
            // 修改
            updateByPrimaryKeySelective(this.dataForm).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('ok')
              this.onClose()
            })
          } else {
            // 新增
            saveHasKey(this.dataForm).then(res => {
              this.$modal.msgSuccess('添加成功')
              this.$emit('ok')
              this.onClose()
            })
          }
        }
      })
    },
    // 客户弹出框回调
    customerDataCallback(data) {
      // 重新校验
      this.dataForm.customerId = data.id
      this.dataForm.customerName = data.customerName
    },
    // 客户input点击事件
    customerIdClick() {
      this.$nextTick(() => {
        this.$refs.customerSelectPopover.open()
      })
    },
    // 合同弹出框回调
    contractDataCallback(data) {
      this.dataForm.contractId = data.id
      this.dataForm.contractSn = data.contractSn
      this.dataForm.contractName = data.contractName
      this.dataForm.money = data.money
    },
    // 合同input点击事件
    contractIdClick() {
      if (this.dataForm.customerId != null) {
        this.$nextTick(() => {
          this.$refs.contractSelectPopover.open('receivables', this.dataForm.customerId)
        })
      }
    }
  }
}

</script>
<style>
</style>
